
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class DAY0330 {
    public static void main(String[] args) {
        ArrayList<Integer>arrayList=new ArrayList<Integer>();
        arrayList.add(-1);
        arrayList.add(3);
        arrayList.add(3);
        arrayList.add(-5);
        arrayList.add(7);
        arrayList.add(4);
        arrayList.add(-9);
        arrayList.add(-7);

        System.out.println("原始数组：");
        System.out.println(arrayList);
//        //反转  revers
//        Collections.reverse(arrayList);
//        System.out.println("Collections.reverse(arrayList):");
//        System.out.println(arrayList);
//        //sort 自然排序的升序
//        Collections.sort(arrayList);
//        System.out.println("Collections.sort(arrayList):");
//        System.out.println(arrayList);

        //定制排序的用法
        Collections.sort(arrayList, new Comparator<Integer>() {
            @Override
            //比较o1和o2的大小
            // 当o1<o2时return -1，
            // o1=o2时return 0
            // o1 > o2时return 1
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });

        System.out.println("定制后：");
        System.out.println(arrayList);
    }

    public int strStr(String haystack, String needle) {
        int n = haystack.length(), m = needle.length();
        if (m == 0) {
            return 0;
        }
        int[] pi = new int[m];
        for (int i = 1, j = 0; i < m; i++) {
            while (j > 0 && needle.charAt(i) != needle.charAt(j)) {
                j = pi[j - 1];
            }
            if (needle.charAt(i) == needle.charAt(j)) {
                j++;
            }
            pi[i] = j;
        }
        for (int i = 0, j = 0; i < n; i++) {
            while (j > 0 && haystack.charAt(i) != needle.charAt(j)) {
                j = pi[j - 1];
            }
            if (haystack.charAt(i) == needle.charAt(j)) {
                j++;
            }
            if (j == m) {
                return i - m + 1;
            }
        }
        return -1;
    }
}
